<?php

require_once __DIR__ . '/../../../vendor/autoload.php';

use JLPay\Core\OrgConfig;
use JLPay\Core\DemoConfig;
use JLPay\Services\Trans\Common\ChnQueryService;
use JLPay\Models\Trans\Common\ChnQueryRequest;
use JLPay\Models\Trans\Common\ChnQueryResponse;
use JLPay\Utils\Logger;
use JLPay\Utils\DemoRequestHandler;

/**
 * 交易 - Common渠道查询功能使用示例
 */

// =================== 工具函数 =====================
/**
 * 创建配置对象
 */
function createConfig(): OrgConfig
{
    return DemoConfig::createConfig();
}

// =================== 数据构建函数 =====================
/**
 * 构建渠道查询请求
 */
function buildChnQueryRequest(): ChnQueryRequest
{
    $request = new ChnQueryRequest();
    
    // 设置请求参数
    $request->setMchId("849584358120018");
    $request->setOutTradeNo("20250327100002zyfTest");
    $request->setTransactionId("451138620256888383078402");
    
    return $request;
}

// =================== 示例函数 =====================
/**
 * 渠道查询示例
 */
function chnQueryDemo(): void
{
    $logger = Logger::getInstance();
    $logger->info("开始执行渠道查询示例");
    
    // 创建配置对象和渠道查询服务
    $config = createConfig();
    $chnQueryService = new ChnQueryService($config);
    
    // 创建渠道查询请求
    $request = buildChnQueryRequest();
    
    // 发送请求 - 使用DemoRequestHandler
    $response = DemoRequestHandler::sendRequestAndHandleResponse(
        $chnQueryService, 
        $request, 
        "渠道查询",
        function($service, $request) {
            return $service->chnQuery($request);
        }
    );
    
    // 处理响应结果
    if ($response) {
        $logger->info("响应报文：" . json_encode($response->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    }
    
    $logger->info("渠道查询示例执行完毕");
}

// =================== 主程序 =====================
$logger = Logger::getInstance();

$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 渠道查询示例 开始 =====");
$logger->info("=" . str_repeat("=", 50));
chnQueryDemo();
$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 渠道查询示例执行完毕 =====");
$logger->info("=" . str_repeat("=", 50)); 